<?php
define("_needlogin", "0");
define("_access", "1");
include_once(dirname(__FILE__)."/../include/db.config.php");
$msg=$_GET['msg'];
if(isset($msg))
{
	$r['hintInterval'] = 300;//倒计时时间s
	if($msg=='begin')
	{
		if (!isset($_SESSION))
			session_start();
		$id=$_GET['id'];
		$_SESSION['hintInterval'] = $r['hintInterval'];
		$now=time();
		if(isset($_SESSION['beginTime'])&&isset($_SESSION['hintId'])&&$id==$_SESSION['hintId'])
		{
			$r['hintInterval']-=($now-$_SESSION['beginTime']);//恢复现场，防止刷新丢失
			if($r['hintInterval']<0)//防止小于0
				$r['hintInterval']=1;
		}
		else
		{
			$_SESSION['beginTime'] = $now;
			$_SESSION['hintInterval'] = $r['hintInterval'];
			$_SESSION['hintId']=$id;
		}
		header('Content-Type: application/json; charset=utf-8');
		echo json_encode($r, JSON_UNESCAPED_UNICODE);
	}
	elseif ($msg=='end')
	{
		if (!isset($_SESSION))
		{
			session_start();
		}
		$endTime= time();
		$id=$_GET['id'];
		$beginTime=$_SESSION['beginTime'];
		if($endTime-$beginTime>=$r['hintInterval']&&isset($id)&&isset($beginTime))//时间间隔正确,其他变量正确定义
		{
			$sql = "SELECT hint from challenge where enable=1 and id=$id";
			$h = array();
			$h= getOne($sql);
			unset($_SESSION['hintId']);//防止作弊使用上次的id
			unset($_SESSION['beginTime']);//防止作弊使用上次的时间
			header('Content-Type: application/json; charset=utf-8');
			echo json_encode($h, JSON_UNESCAPED_UNICODE);
		}else
		{
			$h['hint']='非法操作~~别作弊哦我跟你讲~';
			header('Content-Type: application/json; charset=utf-8');
			echo json_encode($h, JSON_UNESCAPED_UNICODE);
		}
	}
}
